package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("ydd_advpos")
public class YddAdvposController {
    @Autowired
    private IYddAdvposService iYddAdvposService;
    @Autowired
    private IYddAdvimgService iYddAdvimgService;

    //分页查询
    @GetMapping("/list")
    public R list(String advposdesc, String advposname, int page, int limit) {
        log.info("查找的广告位描述有{},名称含{},查询第{}页,每页数量为{}", advposdesc, advposname, page, limit);
        Page<YddAdvpos> yddAdvposPage = new Page<>(page, limit);
        Page<YddAdvposDto> yddAdvposDtoPage = new Page<>(page, limit);
        //创造条件构造器
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper();
        lqw.like(advposdesc != null, YddAdvpos::getAdvposdesc, advposdesc);
        lqw.like(advposname != null, YddAdvpos::getAdvposname, advposname);
        iYddAdvposService.page(yddAdvposPage, lqw);
        //对象拷贝,除了records里的数据,records数据还需要加广告图的数量,需要查询后封装
        BeanUtils.copyProperties(yddAdvposPage, yddAdvposDtoPage, "records");

        List<YddAdvpos> records = yddAdvposPage.getRecords();
        List<YddAdvposDto> yddAdvposDtoList = new ArrayList<>();
        for (YddAdvpos record : records) {
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            BeanUtils.copyProperties(record, yddAdvposDto);

            //创建条件构造器,通过id查询广告图数量
            LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(YddAdvimg::getAdvimgpos, record.getId());
            int count = iYddAdvimgService.count(queryWrapper);

            yddAdvposDto.setAdvposnum(count);
            yddAdvposDtoList.add(yddAdvposDto);
        }
        yddAdvposDtoPage.setRecords(yddAdvposDtoList);

        R r = R.success("data", yddAdvposDtoPage.getRecords());

        r.getData().put("totalPages", (int) yddAdvposDtoPage.getPages());//总页数
        r.getData().put("pageSize", yddAdvposDtoPage.getSize());//每页显示数
        r.getData().put("currentPage", yddAdvposDtoPage.getCurrent());//当前页码值
        r.getData().put("count", (int) yddAdvposDtoPage.getTotal());//总条数

        return r;
    }

    //添加信息
    @PostMapping("/add")
    public R add(@RequestBody YddAdvpos yddAdvpos) {
        log.info("广告位信息为{}", yddAdvpos);
        iYddAdvposService.save(yddAdvpos);
        return R.success("data", "添加成功");
    }

    //回显信息
    @GetMapping("/rowInfo")
    public R rowInfo(int id) {
        log.info("要查询的广告位id为{}", id);
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddAdvpos::getId, id);
        YddAdvpos yddAdvpos = iYddAdvposService.getOne(lqw);
        R r = R.success("id", yddAdvpos.getId());
        r.getData().put("advposname", yddAdvpos.getAdvposname());
        r.getData().put("advposdesc", yddAdvpos.getAdvposdesc());
        r.getData().put("advpossize", yddAdvpos.getAdvpossize());
        r.getData().put("defaultData", yddAdvpos.getDefaultData());
        return r;
    }

    //修改信息
    @PostMapping("/update")
    public R update(@RequestBody YddAdvpos yddAdvpos) {
        log.info("广告位信息为{}", yddAdvpos);
        iYddAdvposService.updateById(yddAdvpos);
        return R.success("data", "修改成功");
    }
    //删除信息
    @GetMapping("/del")
    public R del(Integer id) {
        log.info("要删除的广告位id为{}", id);
        LambdaQueryWrapper<YddAdvimg> lqw =new LambdaQueryWrapper<>();
        lqw.eq(YddAdvimg::getAdvimgpos, id);
        int count = iYddAdvimgService.count(lqw);
        if (count == 0){
            iYddAdvposService.removeById(id);
            return R.success("", "");
        }
        return R.error("该广告位置还有广告不能删除");
    }
    //查看所有
    @GetMapping("/all")
    public Map all() {
        List<YddAdvpos> list = iYddAdvposService.list();
        HashMap map = new HashMap<>();
        map.put("errno", 0);
        map.put("errmsg", "");
        map.put("data", list);
        return map;
    }
}
